package com.meida.module.bus.provider.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.module.bus.provider.service.AppUserExtraService;
import com.meida.module.bus.provider.service.BusProductService;
import com.meida.module.bus.provider.service.BusVipTypeService;
import com.meida.module.product.provider.service.ProdProductdetailsService;
import com.meida.module.user.provider.service.SysHtmlService;
import com.meida.module.user.provider.service.SysSliderService;
import com.meida.module.weixin.provider.config.WxMpProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description: app首页
 * @ClassName IndexController
 * @date: 2022.06.22 08:26
 * @Author: ldd
 */
@RestController
@RequestMapping("/common/index/")
@Api(tags = "首页")
@Log4j2
public class BusIndexController {

    @Autowired
    SysSliderService sysSliderService;

    @Autowired
    BusProductService busProductService;

    @Autowired
    ProdProductdetailsService prodProductdetailsService;

    @Autowired
    SysHtmlService sysHtmlService;

    @Autowired
    BusVipTypeService busVipTypeService;

    @Autowired
    WxMpProperties wxMpProperties;

    @Autowired
    AppUserExtraService appUserService;


    @GetMapping("getOpenId")
    @ApiOperation(value = "获取用户openid", notes = "openid", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", required = true)
    })
    public ResultBody getOpenId(@RequestParam("code") String code) {
        try {
            String appId = wxMpProperties.getConfigs().get(0).getAppId();
            String secret = wxMpProperties.getConfigs().get(0).getSecret();
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".replace("APPID", appId).replace("SECRET", secret).replace("CODE", code);
            String result = HttpUtil.get(url);
            log.info(result);
            return ResultBody.ok(JSON.parseObject(result).getString("openid"));

        } catch (Exception e) {
            log.error(e);
        }
        return ResultBody.failed("获取用户失败");

    }

    @ApiOperation(value = "轮播图-列表", notes = "轮播图列表 ")
    @GetMapping(value = "sliderList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sliderType", required = true, value = "轮播图类型1首页轮播图，2商城轮播图", paramType = "form"),
            @ApiImplicitParam(name = "count", required = true, value = "展示数量", paramType = "form")
    })
    //@LoginRequired(required = false)
    public ResultBody sliderList(@RequestParam(required = true) int sliderType, @RequestParam(required = true) int count) {
        return ResultBody.ok(sysSliderService.listBySliderType(sliderType, count));
    }

    @ApiOperation(value = "商品-列表", notes = "商品列表 ")
    @GetMapping(value = "productList")
    public ResultBody productList() {
        return ResultBody.ok(busProductService.listForIndex());
    }

    @ApiOperation(value = "产品-详情", notes = "产品详情")
    @GetMapping(value = "productDetail")
    public ResultBody productDetail(@RequestParam(required = true) Long productId) {
        return ResultBody.ok(busProductService.getProductDetail(productId));
    }


    @ApiOperation(value = "根据htmlCode查询协议", notes = "根据htmlCode查询协议")
    @GetMapping(value = "findByCode")
    public ResultBody findByCode(String htmlCode) {
        EntityMap map = sysHtmlService.findByHtmlCode(htmlCode);
        return ResultBody.ok(map);
    }

    @ApiOperation(value = "会员列表", notes = "会员类型 ")
    @GetMapping({"vipList"})
    public ResultBody listEntityMap(@RequestParam(required = false) Map param) {
        return this.busVipTypeService.listEntityMap(param);
    }


    @ApiOperation(value = "会员详情", notes = "会员详情")
    @GetMapping(value = "vipDetail")
    public ResultBody vipDetail(@RequestParam(required = true) String vipTypeId) {
        return ResultBody.ok(busVipTypeService.getVipDetail(vipTypeId));
    }


}
